package main

import (
	"context"
	"flag"
	"log"
	"time"

	pb "gitee.com/rs1314/taiqiu/proto/dao"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

var (
	addr    = flag.String("addr", "localhost:60001", "the address to connect to")
	method  = flag.String("method", "AssetList", "AssetList/UserAssetList/UpdateUserAsset")
	userId  = flag.Int("userId", 1, "userId to update")
	assetId = flag.Int("assetId", 1, "assetId to update")
)

func main() {
	flag.Parse()
	// Set up a connection to the server.
	conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewXMetauDaoServiceClient(conn)

	// Contact the server and print out its response.
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	if *method == "AssetList" {
		req := &pb.CommonUserReq{
			UserId: uint64(*userId),
		}
		r, err := c.AssetList(ctx, req)
		if err != nil {
			log.Fatalf("query AssetList failed: %v", err)
		}
		log.Printf("AssetList ok %v", r)
	} else if *method == "UserAssetList" {
		req := &pb.CommonUserReq{
			UserId: uint64(*userId),
		}
		r, err := c.UserAssetList(ctx, req)
		if err != nil {
			log.Fatalf("query user failed: %v", err)
		}
		log.Printf("UserAssetList ok %v", r)
	} else if *method == "UpdateUserAsset" {
		req := &pb.UpdateUserAssetReq{
			UserId:         uint64(*userId),
			AssetId:        uint64(*assetId),
			ExpirationTime: "2033-04-06 13:04:29",
		}
		r, err := c.UpdateUserAsset(ctx, req)
		if err != nil {
			log.Fatalf("query user failed: %v", err)
		}
		log.Printf("UpdateUserAsset ok %v", r)
	}
}
